<script language="JavaScript" type="text/JavaScript">
<!--
// ANIMATION METHODS 
var effectDuration = 500;  // 0.5 second for animation
var reconnectTimeout = 2000; // number of seconds before trying to reconnect
                             // should be at least 2x effectDuration + 1

var slideDiv;
var slideEffect;

var netw_first_done = false;
var netw_issues = false;

function pageTransition(url, delay )
{
	if (slideDiv == null ) slideDiv = document.getElementById('slide');
	if (slideEffect == null) slideEffect = 
		new fx.Opacity(slideDiv , 
		//new fx.Height(slideDiv,
			{ duration: effectDuration }
		);
	
	// after the delay toggle the slideEffect, then sleep during that animation
	// so that the AJAX isn't called to early.
	// During the XMLHttpRequest the screen will stay empty,
	// when the request is finished, toggle the slideEffect so that the screen 
	// is filled with the new page 
	setTimeout(
		function() {
			slideEffect.toggle(); 
			setTimeout( 
				function() {
					loadNextPage(url);
				}, 
				effectDuration);
		
		},  
    	delay);
}

// FIXME: find a way to preload the loading.gif image
function loadNextPage(url)
{
	new Ajax.Updater('slide',
					url, 
					{ 	onComplete:function(){ if (!netw_issues)slideEffect.toggle(); netw_issues=false; },
					    onException:function(){ 
					    	if (!netw_first_done) { // What to do if there is a network issue.
					    	    //alert("Network issue: First message");
					    	    // show a reconnecting text + image
					    	    if (!netw_issues) {
					    	        slideDiv.innerHTML = '<center><p style="padding: 20px 20px 20px 20px;font-size:40px;"><img src="http://localhost/WeIDS/img/loading.gif" /> Reconnecting...</p></center>';
					    	        slideEffect.toggle();
					    	    }
					    	    // try to reconnect
					    	    setTimeout(function(){
					    	                   
					    	                   loadNextPage(url, true);
					    	               },
					    				  reconnectTimeout);
					    	    netw_first_done = true;
					    	    netw_issues = true;
					    	}
					    	else {  // silly method to prevent the network from reconnecting twice
					    	    netw_first_done = false;
					    	}
					    },
						asynchronous:true, 
						evalScripts:true, 
						requestHeaders:['X-Update', 'slide']
					}
	);
}

// -->
</script>
